package com.mv.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mv.mapper.CardMapper;
import com.mv.mapper.UserMapper;
import com.mv.pojo.*;
import com.mv.vo.Form;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class CardServiceImpl implements CardService{
    @Autowired
    private ConsumeService consumeService;
    @Autowired
    private RechargeService rechargeService;
    @Autowired
    private CardService cardService;
    @Autowired
    private MovieService movieService;
    @Autowired
    private DealService dealService;
    @Autowired
    private CardMapper cardMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<Card> FindCard(String query) {
        QueryWrapper<Card> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasLength(query),"id",query);
        return  cardMapper.selectList(queryWrapper);
    }
    @Transactional
    @Override
    public void addCard(Form form) {
        Card card = form.getCard();
        Recharge recharge = form.getRecharge();
        card.setIntegral(0).setStatus(1);
        cardMapper.insert(card);
        recharge.setCardId(card.getId());
        dealService.rechargeDeal(recharge);
    }
    @Transactional
    @Override
    public void deleteCardById(String id) {
        List<Deal> dealList = dealService.getDealListByCardId(id,null,null);
        if(dealList!=null&&dealList.size()!=0){
            for (Deal deal : dealList) {
                if(deal.getTransaction()==1){
                    rechargeService.deleteRechargeByDealId(deal.getId());
                }else {
                    consumeService.deleteConsumeByDealId(deal.getId());
                }
            }
        }
        dealService.deleteDeal(id);
        cardMapper.deleteById(id);
    }
    @Transactional
    @Override
    public Boolean deleteCard(Card card) {
        Integer userId = card.getUserId();
        String id = card.getId();
        cardService.deleteCardById(id);
        List<Card> cardList = cardService.getCardList(userId,null);
        if(cardList==null||cardList.size()==0){
            return true;
        }
        return false;
    }
    @Override
    public List<String> selectId() {
        List<String> idList = new ArrayList<>();
        List<Card> cardList = cardMapper.selectList(null);
        for (Card cardList1:cardList) {
            String id = cardList1.getId();
            idList.add(id);
        }
        return idList;
    }

    @Override
    public Integer getCardCount() {
        return cardMapper.selectCount(null);
    }

    @Override
    public List<Card> getCardList(Integer userId,String query) {
        QueryWrapper<Card> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId)
                .like(StringUtils.hasLength(query),"id",query);
        return cardMapper.selectList(queryWrapper);
    }
    @Transactional
    @Override
    public void updateStatus(Card card) {
        if(card.getStatus()==0){
            card.setStatus(1);
        }else {
            card.setStatus(0);
        }
        cardMapper.updateById(card);
    }
    @Transactional
    @Override
    public void updateMoney(Form form) {
        Card card = form.getCard();
        card.setMoney(card.getMoney()+card.getPrice());
        cardMapper.updateById(card);
        dealService.rechargeDeal(form.getRecharge());
    }

    @Override
    public String getUserNameByUserId(Integer userId) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", userId);
        return userMapper.selectOne(queryWrapper).getUserName();
    }
    @Transactional
    @Override
    public void updateCard(Form form) {
        Card card = form.getCard();
        Movie movie = form.getMovie();
        card.setMoney(card.getMoney()-movie.getPrice()*movie.getNum())
                .setIntegral(card.getIntegral()+movie.getIntegral()*movie.getNum());
        cardMapper.updateById(card);
        movieService.addMovieCount(movie);
        dealService.consumeDeal(card,movie);
    }
    @Transactional
    @Override
    public void deleteCardByUserId(Integer userId) {
        QueryWrapper<Card> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        cardMapper.delete(queryWrapper);
    }
    @Transactional
    @Override
    public void deleteBySelectList(List<User> selectList) {
        QueryWrapper<Card> queryWrapper = new QueryWrapper<>();
        for (User user : selectList) {
            queryWrapper.eq("user_id", user.getId());
            cardMapper.delete(queryWrapper);
            queryWrapper.clear();
        }
    }
    @Override
    public List<Card> getCardId() {
        List<Card> cardList=new ArrayList<>();
        QueryWrapper<Card> queryWrapper = new QueryWrapper<>();
        List idList = cardMapper.selectObjs(null);
        List<String> list=(List<String>) idList;
        for (String id : list) {
            cardList.add(new Card().setId(id).setFlag(false));
        }
        return cardList;
    }
    @Override
    public Card getCardById(String id) {
        return cardMapper.selectById(id);
    }
    @Override
    public List<User> getUserAndCard() {
        QueryWrapper<Card> queryWrapper = new QueryWrapper<>();
        List<User> userList = userMapper.selectList(null);
        for (User user : userList) {
            Integer userId = user.getId();
            queryWrapper.eq("user_id", userId);
            List<Card> cardList = cardMapper.selectList(queryWrapper);
            queryWrapper.clear();
            ArrayList<User> cardIdList = new ArrayList<>();
            for (Card card : cardList) {
                cardIdList.add(new User().setUserName(card.getId()));
            }
            user.setChildren(cardIdList);
        }
        return userList;
    }

    @Override
    public Double getMoney(String id) {
        return cardService.getCardById(id).getMoney();
    }

    @Override
    public List<String> getCardIdByUserId(Integer id) {
        List<String> cardList=cardMapper.getCardId(id);
        return cardList;
    }
}
